package org.hackystat.sdsa.analyzer.model.activity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;

import jess.Fact;

import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Test;

/**
 * Test buffer transitional action.
 * 
 * @author Hongbing Kou
 */
public class TestBufferTransActivity extends ActivityTestBase {
  /** Sensor data. */
  private SensorData sensorData;
  /** Logger. */
  private Logger logger;
  
  /**
   * Prepares for testing.
   * 
   * @throws Exception If error occurs.
   */
  @Override public void setUp() throws Exception {
    super.setUp();
    
    this.sensorData = new SensorData();
    XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2005-12-01T08:30:00.000-10:00");
    sensorData.setTimestamp(tstamp);
    sensorData.setResource("file:/C:/cvs/work/com/foo/Bar.java");
    sensorData.setSensorDataType("DevEvent");
    
    this.logger = Logger.getLogger(TestBufferTransActivity.class.getName());
  }
  
  /**
   * Test basic function of buffer transition.
   * 
   * @throws Exception If error while testing.
   */
  @Test public void testBuffTrans() throws Exception {
    BufferTransActivity action = new BufferTransActivity(this.sensorData, this.logger);
    
    action.setFromResource("file:/c:/cvs/work/com/foo/TestBar.java");
    assertEquals("Test from resource", "TestBar.java", 
        action.extractFileName(action.getFromResource()));
    assertEquals("Test buff trans string", "2005-12-01T08:30:00.000-10:00 Bar.java BUFFTRANS " + 
                                           "{FROM=TestBar.java}", action.toString());
  }

  /**
   * Tests conversion from action object to JESS fact.
   * 
   * @throws Exception If error while converting to Jess fact.
   */
  @Override public void testJessFactConversion() throws Exception {
    BufferTransActivity action = new BufferTransActivity(this.sensorData, this.logger);
    action.setFromResource("file:/c:/cvs/work/com/foo/TestFoo.java");
    Fact f = action.assertActivity(1, engine);

    assertTrue("Test action is asserted to working memory", f.getFactId() >= 0);
  }
}
